package com.mirror.llk.view;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import cn.waps.AdView;

import com.mirror.llk.R;
import com.mirror.llk.bean.domain.TTheme;
import com.mirror.llk.utils.Constant;
import com.mirror.utils.base.VerifyResult;
import com.mirror.utils.io.FileUtils;
import com.mirror.utils.lang.StringUtils;
import com.mirror.utils.ui.UiUtils;

public class DiyView extends LinearLayout {

	private static final String TAG = "DiyView";

	private int photoNum;

	private Context context;

	public EditText etName;

	public EditText etDesc;

	public Button btnClear;

	public Button btnSave;

	public SeekBar skChoose;

	public LinearLayout llPhotoArea;

	public List<ImageView> ivList = new ArrayList<ImageView>();

	public DiyView(Context context) {
		this(context, null);
	}

	@SuppressWarnings("deprecation")
	public DiyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.theme_diy, this, true);
		setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
		this.init();
	}

	private void init() {

		llPhotoArea = (LinearLayout) this.findViewById(R.id.ll_diy_photoArea);

		initPhotoArea(100);

		etName = (EditText) this.findViewById(R.id.et_themeDiy_themeName);
		etDesc = (EditText) this.findViewById(R.id.et_themeDiy_themeDesc);

		btnClear = (Button) this.findViewById(R.id.btn_themeDiy_clear);
		btnSave = (Button) this.findViewById(R.id.btn_themeDiy_save);

		skChoose = (SeekBar) this.findViewById(R.id.sk_themeDiy_choose);

		// 互动广告调用方式
		LinearLayout container = (LinearLayout) findViewById(R.id.adsContainer);
		new AdView(context, container).DisplayAd(10);
	}

	public void initPhotoArea(int dimension) {
		photoNum = dimension / 5;
		if (photoNum == ivList.size()) {
			return;
		}
		clearPhotoArea();
		Random random = new Random(254);
		int col = UiUtils.px2dip(context, UiUtils.getDisplaySize(context)[0]) / 70;
		int row = photoNum / col + (photoNum % col == 0 ? 0 : 1);
		int space = (UiUtils.px2dip(context, UiUtils.getDisplaySize(context)[0]) % 70) / (col + 1);
		for (int i = 0; i < row; i++) {
			if (ivList.size() == photoNum) {
				return;
			}
			LinearLayout layout = new LinearLayout(context);
			layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, UiUtils.dip2px(context, 90)));
			layout.setOrientation(HORIZONTAL);
			layout.setGravity(Gravity.CENTER);
			layout.setBackgroundColor(Color.argb(0, random.nextInt(), random.nextInt(), random.nextInt()));
			initRow(layout, i, col, space);
			llPhotoArea.addView(layout);
		}
	}

	private void initRow(LinearLayout layout, int row, int col, int space) {
		for (int i = 0; i < col; i++) {
			if (ivList.size() == photoNum) {
				return;
			}
			ImageView imageView = new ImageView(context);
			imageView.setId(row * col + i + 1);
			LayoutParams layoutParams = new LayoutParams(UiUtils.dip2px(context, 70), UiUtils.dip2px(context, 70));
			layoutParams.setMargins(space, 0, 0, 0);
			imageView.setLayoutParams(layoutParams);
			imageView.setBackgroundResource(R.drawable.update_photo);
			layout.addView(imageView);
			ivList.add(imageView);
		}
	}

	public void setImage(ImageView imageView) {
		String fileName = "IMG_" + imageView.getId() + ".png";
		String path = FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE + fileName;
		File file = new File(path);
		if (file.exists()) {
			Bitmap photo = FileUtils.convertToBitmap(path, 100, 100);
			imageView.setImageBitmap(photo);
		}
	}

	public void clearImage(ImageView imageView) {
		imageView.setImageDrawable(getResources().getDrawable(R.drawable.update_photo));
	}

	public VerifyResult<TTheme> verifySubmit() throws Exception {
		String themeName = etName.getText().toString().trim();
		String themeDesc = etDesc.getText().toString().trim();
		if (StringUtils.isEmpty(themeName) || StringUtils.isEmpty(themeDesc) || !verifyPicture()) {
			return new VerifyResult<TTheme>(false, "亲,主题还不完整哦...");
		}
		return new VerifyResult<TTheme>(true, VerifyResult.PASS, new TTheme(themeName, themeDesc, TTheme.THEME_TYPE_PRIVATE, 1L));
	}

	private boolean verifyPicture() throws Exception {
		String path = FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE;
		File dir = new File(path);
		if (dir == null || !dir.isDirectory()) {
			FileUtils.createDir(path);
			return false;
		}
		int i = 0;
		for (File f : dir.listFiles()) {
			if (f.getName().startsWith("IMG_") && f.getName().endsWith(".png")) {
				i++;
			}
		}
		if (i == photoNum) {
			return true;
		} else if (i < photoNum) {
			return false;
		} else {
			FileUtils.clearDir(Constant.LOCATION_HIGH_PHOTO, Constant.DIR_TEMP_FILE);
			throw new Exception();
		}
	}

	public void clearPhotoArea() {
		// 清空临时目录
		FileUtils.clearDir(FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE);
		ivList.clear();
		llPhotoArea.removeAllViews();
	}
}
